In [4]:
def gensquares(N):
for num in xrange(1,N):
yield num**2
In [5]:
for x in gensquares(10):
print x
In [6]:
import random
random.randint(1,10)
Out[6]:
In [7]:
def rand_num(low,high,n):
for num in xrange(n):
yield random.randint(low,high)
In [8]:
for num in rand_num(1,10,12):
print num
In [12]:
s = 'hello'
s_iter = iter(s)
next(s_iter)
next(s_iter)
#code here
Out[12]:
loop over a large matrix or data frame
In [13]:
my_list = [1,2,3,4,5]
gencomp = (item for item in my_list if item > 3)
for item in gencomp:
print item
In [ ]: